#include "sdk.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "byogames.h"
#include "byogame.h"
#include "byogamebase.h"
#include "byogameselect.h"
#include "byoconf.h"

int idGameSelect = XRCID( "idGameSelect" );

BEGIN_EVENT_TABLE( Games, M_Code_Plugin )
  EVT_MENU( idGameSelect, Games::OnOnGameSelect )
  EVT_TIMER( 1, Games::OnTimer )
END_EVENT_TABLE()

namespace {
  PluginRegistrant<Games> reg( _T( "Games" ) );
};

Games::Games(): SecondTick( this, 1 ) {
  SecondTick.Start( 1000, true );
}

Games::~Games() {
}

void Games::OnAttach() {
  srand( time( NULL ) );
  GameBase::ReloadFromConfig();
}

void Games::OnRelease( bool ) {
}

int Games::Execute() {
  int gameNum = SelectGame();
  if( gameNum < 0 || gameNum >= GameLauncher::GetGamesCount() ) {
    return 0;
  }
  GameLauncher::PlayGame( gameNum );
  return 0;
}

int Games::SelectGame() {
  GameSelect Select( 0 );
  return Select.ShowModal();
}

void Games::OnTimer( wxTimerEvent& ) {
  GameBase::BackToWorkTimer();
  SecondTick.Start( -1, true );
}

cbConfigurationPanel* Games::GetConfigurationPanel( wxWindow* parent ) {
  return new byoConf( parent );
}

void Games::OnOnGameSelect( wxCommandEvent& event ) {
  SelectGame();
}
